//============================================================================
// Name        : util.cpp
// Author      : bycy
// Version     : 1.0
// Description : 
//============================================================================



/*
* if_define
*
* test if from defines relevant variable of value
*
* REQUIRE:	value NOT NULL
*
* RETURN:	ture if from does define relevant variable of value
*		false if from does not define relevant variable of value
*
* MODIFY:	
*/

bool if_define(vector<opnd_t> from, opnd_t value){

	for(int i = 0; i < from.size(); i++){

		opnd_t temp = from[i];

		if(opnd_same(temp, value){

			return true;
			
		}

	}

	return false;

}

/*
* if_define
*
* test if temp_from define relevant variable of temp_to
*
* REQUIRE:	temp_from NOT NULL
*		temp_to NOT NULL
*
* RETURN:	ture if temp_from does define relevant variable of temp_to
*		false if temp_from does not define relevant variable of temp_to
*
* MODIFY:	
*/

bool if_define(instruction_t *temp_from, instruction_t *temp_to){

	vector<opnd_t> rel_vals_from = temp_from->rel_vals;

	vector<opnd_t> rel_vals_to = temp_to->rel_vals;

	for(int i = 0; i < rel_vals_to.size(); i++){

		opnd_t temp = rel_vals_to[i];

		if(if_define(rel_vals_from, temp){

			return true;

		}

	}

	return false;	

}

/*
* if_contain
*
* test if vector from contains opnd_t value
*
* REQUIRE:	value NOT NULL
*
* RETURN:	ture if vector from does not contain opnd_t value
*		false if vector from does not contain opnd_t value
*
* MODIFY:	from
*/

bool if_contain(vector<opnd_t> from, opnd_t value){
 
	bool contain = false;
	
	for(int i = 0; i < from.size(); i++){

		opnd_t temp = from[i];

		if(opnd_same(temp, value){

			contain = true;
			
		}

	}

	return contain;

}

/*
* if_contain
*
* test if vector from contains instr_t * value
*
* REQUIRE:	value NOT NULL
*
* RETURN:	ture if vector from does not contain instr_t * value
*		false if vector from does not contain instr_t * value
*
* MODIFY:	from
*

bool if_contain(vector<instr_t *> from, instr_t * value){
 
	bool contain = false;
	
	for(int i = 0; i < from.size(); i++){

		instr_t * temp = from[i];

		if(instr_same(temp, value){

			contain = true;
			
		}

	}

	return contain;

}